<!DOCTYPE html>
<?php
	session_start();
?>
<html>
	<head>
		<meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
		<title>我的主页</title>
		<link rel = "stylesheet" href = "base.css">
		<style>
			div#menu {
				height: 319px;
				width: 690px;
				margin-top: 100px;
				text-align: center;
				float: left;
			}
			div#content {
				height: 319px;
				width: 500px;
				float: left;
			}
			div#login {
				height: 219px;
				width: 300px;
				border: 1px solid #cbcbcb;
				background: #fff;
				text-align: center;
				margin-top: 100px; 
			}
			h2 {
				font-size: 18px;
			}
			a:link{
				text-decoration: none;
				color: black;
			}
			a:visited { 
				text-decoration: none;
				color: black;
			}
		</style>
	</head>
	<?php
		include("conn.php");
	?>
	<body class = "content">
		<div id="container" >
			<div>
				<h1><img src = "image/logo.gif"  width = "150px" height = "50px"/></h1>
				<hr>
			</div>
			<div id="menu">
				<img src = "image/1.gif" width = "400px" height = "150px"	/>
			</div>
			<div id="content">
			<form action="" method="post">
			<div id = "login">
			<div><h2>用户登录</h2></div>
			<br/>
			<div>账户<input type = "text" name = "username"></div>
			<div>密码<input type = "password" name = "password"></div>
			<br/>
			<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<input type = "submit" name = "login" value = "登录">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<button type = "buttom"><a href = "register.php">注册</a></button></div>
			</div>
			</form>
		</div>
		<div id="footer"><hr>Copyright © 2014 xiaolongge</div>
		</div>
		<?php
			if (isset($_POST["login"]))
			{
				if ($_POST["username"] == "")
				{
					echo "<script>alert('请输入用户名');</script>";
				}
				else if ($_POST["password"] == "")
				{
					echo "<script>alert('请输入密码');</script>";
				}
				else
				{
					$username = $_POST["username"];
					$sql="SELECT username, password,count FROM user WHERE username = '$_POST[username]' and password = '$_POST[password]'";
					$result = mysql_query($sql);
					$num = mysql_num_rows($result);
					if ($num > 0)
					{
						$_SESSION['user'] = $_POST["username"];
						$row = mysql_fetch_array($result);
						$count = $row["count"];
						++$count;
						$sql = "UPDATE user SET count = '$count' WHERE username = '$_POST[username]'";
						mysql_query($sql);
						echo "<script>alert('登录成功！这是你第 $count 次登录！');location.href='index2.php';</script>";
					}
					else
					{
						echo "<script>alert('用户名或密码错误！');</script>";
					}
				}
			}
		?>

	</body>
</html>